package org.rescue.web;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.io.*;

import org.rescue.analysis.*;
import org.rescue.analysis.summarize.SummarizeAnalysis;
import org.rescue.analysis.summarize.GreedySummarizor;
import org.rescue.analysis.taxonomy.ConceptToRootReader;
import org.rescue.analysis.taxonomy.LocationCToRReader;
import org.rescue.analysis.taxonomy.SummarizeSubTaxonomyDB;
import org.rescue.analysis.taxonomy.TaxonomyDB;
import org.rescue.search.*;

public class Analyze extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		

		int k = Integer.valueOf(request.getParameter("k")).intValue();	
		int datasetId = Integer.valueOf(request.getParameter("datasets")).intValue();
		String analysisType = request.getParameter("analysis_type");
		//int taxonomyId = Integer.valueOf(request.getParameter("taxonomyId")).intValue();
		
		
		DatasetDB datasetDB = new DatasetDB();
		Dataset d = datasetDB.readDataset(datasetId);
		
		
		SummarizeSubTaxonomyDB taxonomyReader = new SummarizeSubTaxonomyDB();
		ConceptToRootReader ctorReader = new LocationCToRReader();
		
		DefaultMutableTreeNode testTaxonomy;
		testTaxonomy = taxonomyReader.readTaxonomy(ctorReader, d);
		taxonomyReader.printTaxonomy(testTaxonomy);
		
		GreedySummarizor summarizor = new GreedySummarizor();
		DefaultMutableTreeNode result = null;
		result = summarizor.summarize (testTaxonomy, k);	
		
		
		DatasetAnalysis da = new DatasetAnalysis();
		AnalysisResult ar = new AnalysisResult();
		ar.setResultTree(result);
		ar.setResultTaxonomy(testTaxonomy);
		da.setResult(ar);
		
		
		/*DatasetDB datasetDB = new DatasetDB();
		Dataset d = datasetDB.readDataset(datasetId);

		//TaxonomyDB taxonomyDB = new TaxonomyDB();
		//DefaultMutableTreeNode t = taxonomyDB.readTaxonomy(taxonomyId);
		//d.setTaxonomy(t);
		//d.countConcepts();
			
		DatasetAnalysis da = new DatasetAnalysis();
		if (analysisType.equals("summarize")) {
			da.setAnalysisOperation(new SummarizeAnalysis());
		}
		else if (analysisType.equals("compare")) {

		}
		else if (analysisType.equals("discover")) {

		}

		da.performAnalysis(d,k);
			*/
		request.setAttribute("dataset_analysis", da);	
		RequestDispatcher view = request.getRequestDispatcher("analyze_results.jsp");
		view.forward(request,response);
			
		
	}
	
}
